package com.zxx.pattern.buildPattern.build4_ZZ;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * HtmlBuilder 实现类：
 * @author zxx on 2020/12/11
 */
public class HtmlBuilder extends Builder4 {

    private String fileName;
    private PrintWriter pw;

    @Override
    public void makeString(String string) {
        pw.println("<p>" + string + "<p>");
    }

    @Override
    public void makeTitle(String title) {
        fileName = "D:\\" + title + ".html";

        try {
            pw = new PrintWriter(new FileWriter(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        pw.println("<html><head><title>" + title + "</title></head><body>");
        pw.println("<h1>" + title + "</h1>");
    }

    @Override
    public void makeItems(String[] items) {
        pw.println("<ul>");
        for (String item : items) {
            pw.println("<li>" + item + "</li>");
        }
        pw.println("</ul>");
    }

    @Override
    public void close() {
        pw.println("</body></html>");
        pw.close();
    }

    public String getFileName() {
        return fileName;
    }
}
